﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Presenter.Users;

namespace SocialNetworkClient.Pages.User
{
    public partial class SignUp : System.Web.UI.Page, ISignUpView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["SignUpPresenter"] != null)
            {
                var presenter = Session["SignUpPresenter"] as SignUpPresenter;
                if (presenter != null)
                    presenter.Reload(this);
            }
            else
            {
                Session["SignUpPresenter"] = new SignUpPresenter(this);
            }
        }

        public void CreateLbEntityButtonClick(object sender, EventArgs e)
        {
            if (EntityCreated != null)
            {
                var interests = new Dictionary<string, string>();
                if (!string.IsNullOrEmpty(Interest1Tb.Text))
                    interests.Add(Interest1Tb.Text, (Session["ParentInterest1DdlSelectedIndex"] != null && (int)Session["ParentInterest1DdlSelectedIndex"] != -1) ? ParentInterest1Ddl.Items[(int)Session["ParentInterest1DdlSelectedIndex"]].Text : string.Empty);
                if (!string.IsNullOrEmpty(Interest2Tb.Text) && Interest2Tb.Text != Interest1Tb.Text)
                    interests.Add(Interest2Tb.Text, (Session["ParentInterest2DdlSelectedIndex"] != null && (int)Session["ParentInterest2DdlSelectedIndex"] != -1) ? ParentInterest2Ddl.Items[(int)Session["ParentInterest2DdlSelectedIndex"]].Text : string.Empty);
                
                    EntityCreated(EmailTb.Text, 
                              PasswordTb.Text,
                              NicknameTb.Text,
                              FullnameTb.Text,
                              JobTb.Text,
                              Convert.ToDateTime(BirthdayTb.Text),
                              interests);
            }

            if (CanSignIn)
                Page.Response.Redirect(@"~/Pages/User/Home.aspx");

            Session.Remove("ParentInterest2DdlSelectedIndex");
            Session.Remove("ParentInterest1DdlSelectedIndex");
        }

        #region Implementation of ISignUpView

        public event Action<string, string, string, string, string, DateTime, Dictionary<string, string>> EntityCreated;

        public bool CanSignIn { get; set; }

        public void SetSessionParameters(int id, string email, string password)
        {
            if (Session["Email"] == null && Session["Password"] == null && Session["UserId"] == null)
            {
                Session["Email"]    = email;
                Session["Password"] = password;
                Session["UserId"]   = id.ToString();
            }
        }

        public void GetAllInterests(Dictionary<int, string> interests)
        {
            Session["ParentInterest2DdlSelectedIndex"] = ParentInterest2Ddl.SelectedIndex;
            Session["ParentInterest1DdlSelectedIndex"] = ParentInterest1Ddl.SelectedIndex;

            ParentInterest1Ddl.Items.Clear(); 
            ParentInterest2Ddl.Items.Clear();

            ParentInterest1Ddl.Items.Add(new ListItem("", ""));
            ParentInterest2Ddl.Items.Add(new ListItem("", ""));

            if (interests != null)
                foreach (var obj in interests)
                {
                    ParentInterest1Ddl.Items.Add(new ListItem(obj.Value, obj.Key.ToString()));
                    ParentInterest2Ddl.Items.Add(new ListItem(obj.Value, obj.Key.ToString()));
                }

            if (Session["ParentInterest2DdlSelectedIndex"] != null)
                ParentInterest2Ddl.SelectedIndex = (int) Session["ParentInterest2DdlSelectedIndex"];
            if (Session["ParentInterest1DdlSelectedIndex"] != null)
                ParentInterest1Ddl.SelectedIndex = (int)Session["ParentInterest1DdlSelectedIndex"];
        }

        #endregion
    }
}
